unit DecryptorsGui;

interface

uses
  Decryptor, InventionCalc, GlobalRecordsAndEnums, ImportPricesGui, Items,

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TfrmDecryptors = class(TForm)
    lstDecryptors: TListView;
    gbDecryptor: TGroupBox;
    lblName: TLabel;
    lblItemName: TLabel;
    lblPrice: TLabel;
    edtItemPrice: TEdit;
    btnClose: TButton;
    btnImport: TButton;
    btnHelp: TButton;
    procedure lstDecryptorsChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure edtItemPriceChange(Sender: TObject);
    procedure btnImportClick(Sender: TObject);
    procedure btnHelpClick(Sender: TObject);
  private
    inventionCalc: TInventionCalc;
    loading: Boolean;
    procedure LoadDecryptors;
  public
    constructor Create(AOwner: TComponent; invCalc: TInventionCalc); reintroduce;
  end;

var
  frmDecryptors: TfrmDecryptors;

implementation

{$R *.dfm}

uses
  StrUtils, ExtActns;

constructor TfrmDecryptors.Create(AOwner: TComponent; invCalc: TInventionCalc);
begin
  inherited Create(AOwner);
  inventionCalc := invCalc;

  LoadDecryptors;
end;

procedure TfrmDecryptors.LoadDecryptors;
var
  i: Integer;
  decryptor: TDecryptor;
  item: TListItem;
begin
  loading := true;
  lstDecryptors.Items.Clear;
  for i := 0 to inventionCalc.AmarrDecryptorCount -1 do begin
    decryptor := inventionCalc.GetDecryptor(Amarr, i);
    item := lstDecryptors.Items.Add;
    item.Caption := 'Amarr';
    item.Data := decryptor;
    item.SubItems.Add(decryptor.Name);
    item.SubItems.Add(FloatToStrF(decryptor.Price, ffNumber, 11, 2));
    item.SubItems.Add(FloatToStr(decryptor.Probability));
    item.SubItems.Add(IntToStr(decryptor.ME));
    item.SubItems.Add(IntToStr(decryptor.PE));
    item.SubItems.Add(IntToStr(decryptor.Runs));
  end;
  for i := 0 to inventionCalc.CaldariDecryptorCount -1 do begin
    decryptor := inventionCalc.GetDecryptor(Caldari, i);
    item := lstDecryptors.Items.Add;
    item.Caption := 'Caldari';
    item.Data := decryptor;
    item.SubItems.Add(decryptor.Name);
    item.SubItems.Add(FloatToStrF(decryptor.Price, ffNumber, 11, 2));
    item.SubItems.Add(FloatToStr(decryptor.Probability));
    item.SubItems.Add(IntToStr(decryptor.ME));
    item.SubItems.Add(IntToStr(decryptor.PE));
    item.SubItems.Add(IntToStr(decryptor.Runs));
  end;
  for i := 0 to inventionCalc.GallenteDecryptorCount -1 do begin
    decryptor := inventionCalc.GetDecryptor(Gallente, i);
    item := lstDecryptors.Items.Add;
    item.Caption := 'Gallente';
    item.Data := decryptor;
    item.SubItems.Add(decryptor.Name);
    item.SubItems.Add(FloatToStrF(decryptor.Price, ffNumber, 11, 2));
    item.SubItems.Add(FloatToStr(decryptor.Probability));
    item.SubItems.Add(IntToStr(decryptor.ME));
    item.SubItems.Add(IntToStr(decryptor.PE));
    item.SubItems.Add(IntToStr(decryptor.Runs));
  end;
  for i := 0 to inventionCalc.MinmatarDecryptorCount -1 do begin
    decryptor := inventionCalc.GetDecryptor(Minmatar, i);
    item := lstDecryptors.Items.Add;
    item.Caption := 'Minmatar';
    item.Data := decryptor;
    item.SubItems.Add(decryptor.Name);
    item.SubItems.Add(FloatToStrF(decryptor.Price, ffNumber, 11, 2));
    item.SubItems.Add(FloatToStr(decryptor.Probability));
    item.SubItems.Add(IntToStr(decryptor.ME));
    item.SubItems.Add(IntToStr(decryptor.PE));
    item.SubItems.Add(IntToStr(decryptor.Runs));
  end;
  loading := false;
  if (lstDecryptors.Items.Count > 0) then lstDecryptors.Selected := lstDecryptors.Items[0]
  else begin
    edtItemPrice.Enabled := false;
  end;
end;

procedure TfrmDecryptors.lstDecryptorsChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
var
  decryptor: TDecryptor;
begin
  if (lstDecryptors.Selected <> nil) then begin
    if (not loading) then begin
      decryptor := TDecryptor(Item.Data);
      lblItemName.Caption := decryptor.Name;
      edtItemPrice.Text := FloatToStr(decryptor.Price);
    end;
  end;
end;

procedure TfrmDecryptors.edtItemPriceChange(Sender: TObject);
var
  price: Double;
  selecteditem: TDecryptor;
begin
  if (lstDecryptors.Selected <> nil) then begin
    selecteditem := TDecryptor(lstDecryptors.Selected.Data);
    if ((selecteditem <> nil) and (Length(edtItemPrice.Text) > 0)) then begin
      if (TryStrToFloat(edtItemPrice.Text, price)) then begin
        selecteditem.Price := price;
        lstDecryptors.Selected.Subitems[1] := FloatToStrF(selecteditem.Price, ffNumber, 11, 2);
      end
      else begin
        edtItemPrice.Text := CorrectPrice(edtItemPrice.Text);
        edtItemPrice.SelStart := Length(edtItemPrice.Text);
      end;
    end;
  end;
end;

procedure TfrmDecryptors.btnHelpClick(Sender: TObject);
var
  run: TFileRun;
begin
  run := TFileRun.Create(self);
  run.FileName := 'help\Decryptors.html';
  run.Execute;
  run.Free;
end;

procedure TfrmDecryptors.btnImportClick(Sender: TObject);
var
  i, len: Integer;
  gui: TfrmImportPrices;
  itemslist: TNewItemsList;
begin
  SetLength(itemslist, 0);
  for i := 0 to lstDecryptors.Items.Count -1 do begin
    len := Length(itemslist);
    SetLength(itemslist, len +1);
    itemslist[len] := TNewItem(lstDecryptors.Items[i].Data);
  end;
  gui := TfrmImportPrices.Create(self, itemslist, inventioncalc);
//  gui.ShowModal;
  if (gui.ShowModal <> mrCancel) then LoadDecryptors;
  gui.Destroy;
end;
end.
